home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / bfd / archive.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-02  |  40.6 KB  |  1,471 lines

  1. /* BFD back-end for archive files (libraries).
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.  Mostly Gumby Henkel-Wallace's fault.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /*
  22. @setfilename archive-info
  23. SECTION
  24.     Archives
  25.  
  26. DESCRIPTION
  27.     Archives are supported in BFD in <<archive.c>>.
  28.  
  29.     An archive (or library) is just another BFD.  It has a symbol
  30.     table, although there's not much a user program will do with it.
  31.  
  32.     The big difference between an archive BFD and an ordinary BFD
  33.     is that the archive doesn't have sections.  Instead it has a
  34.     chain of BFDs considered its contents.  These BFDs can be
  35.     manipulated just like any other.  The BFDs contained in an
  36.     archive opened for reading will all be opened for reading; you
  37.     may put either input or output BFDs into an archive opened for
  38.     output; it will be handled correctly when the archive is closed.
  39.  
  40.     Use <<bfd_openr_next_archived_file>> to step through all
  41.     the contents of an archive opened for input.  It's not
  42.     required that you read the entire archive if you don't want
  43.     to!  Read it until you find what you want.
  44.  
  45.     Archive contents of output BFDs are chained through the
  46.     <<next>> pointer in a BFD.  The first one is findable through
  47.     the <<archive_head>> slot of the archive.  Set it with
  48.     <<set_archive_head>> (q.v.).  A given BFD may be in only one
  49.     open output archive at a time.
  50.  
  51.     As expected, the BFD archive code is more general than the
  52.     archive code of any given environment.  BFD archives may
  53.     contain files of different formats (eg a.out and coff) and
  54.     even different architectures.  You may even place archives
  55.     recursively into archives!
  56.  
  57.     This can cause unexpected confusion, since some archive
  58.     formats are more expressive than others.  For instance intel
  59.     COFF archives can preserve long filenames; Sun a.out archives
  60.     cannot.  If you move a file from the first to the second
  61.     format and back again, the filename may be truncated.
  62.     Likewise, different a.out environments have different
  63.     conventions as to how they truncate filenames, whether they
  64.     preserve directory names in filenames, etc.  When
  65.     interoperating with native tools, be sure your files are
  66.     homogeneous.
  67.  
  68.     Beware: most of these formats do not react well to the
  69.     presence of spaces in filenames.  We do the best we can, but
  70.     can't always handle this due to restrctions in the format of
  71.     archives.  Many unix utilities are braindead in regards to
  72.     spaces and such in filenames anyway, so this shouldn't be much
  73.     of a restriction.
  74. */
  75.  
  76. /* Assumes:
  77.    o - all archive elements start on an even boundary, newline padded;
  78.    o - all arch headers are char *;
  79.    o - all arch headers are the same size (across architectures).
  80. */
  81.  
  82. /* Some formats provide a way to cram a long filename into the short
  83.    (16 chars) space provided by a bsd archive.  The trick is: make a
  84.    special "file" in the front of the archive, sort of like the SYMDEF
  85.    entry.  If the filename is too long to fit, put it in the extended
  86.    name table, and use its index as the filename.  To prevent
  87.    confusion prepend the index with a space.  This means you can't
  88.    have filenames that start with a space, but then again, many unix
  89.    utilities can't handle that anyway.
  90.  
  91.    This scheme unfortunately requires that you stand on your head in
  92.    order to write an archive since you need to put a magic file at the
  93.    front, and need to touch every entry to do so.  C'est la vie.
  94. */
  95.  
  96. /* $Id: archive.c,v 1.47 1992/04/02 07:26:07 gnu Exp $ */
  97.  
  98. #include "bfd.h"
  99. #include "sysdep.h"
  100. #include "libbfd.h"
  101. #include "aout/ar.h"
  102. #include "aout/ranlib.h"
  103.  
  104. #ifdef GNU960
  105. #define BFD_GNU960_ARMAG(abfd)    (BFD_COFF_FILE_P((abfd)) ? ARMAG : ARMAGB)
  106. #endif
  107.  
  108. /* We keep a cache of archive filepointers to archive elements to
  109.    speed up searching the archive by filepos.  We only add an entry to
  110.    the cache when we actually read one.  We also don't sort the cache;
  111.    it's generally short enough to search linearly.
  112.    Note that the pointers here point to the front of the ar_hdr, not
  113.    to the front of the contents!
  114. */
  115. struct ar_cache {
  116.   file_ptr ptr;
  117.   bfd* arelt;
  118.   struct ar_cache *next;
  119. };
  120.  
  121. #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
  122. #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
  123.  
  124. #define arch_hdr(bfd) ((struct ar_hdr *)   \
  125.                (((struct areltdata *)((bfd)->arelt_data))->arch_header))
  126.  
  127. boolean
  128. _bfd_generic_mkarchive (abfd)
  129.      bfd *abfd;
  130. {
  131.   abfd->tdata.aout_ar_data = (struct artdata *)bfd_zalloc(abfd, sizeof (struct artdata));
  132.  
  133.   if (bfd_ardata (abfd) == NULL) {
  134.       bfd_error = no_memory;
  135.       return false;
  136.     }
  137.   bfd_ardata(abfd)->cache = 0;
  138.   return true;
  139. }
  140.  
  141. /*
  142. FUNCTION
  143.     bfd_get_next_mapent
  144.  
  145. SYNOPSIS
  146.     symindex bfd_get_next_mapent(bfd *, symindex previous, carsym ** sym);
  147.  
  148. DESCRIPTION
  149.     This function steps through an archive's symbol table (if it
  150.     has one).  Successively updates <<sym>> with the next symbol's
  151.     information, returning that symbol's (internal) index into the
  152.     symbol table.
  153.  
  154.     Supply BFD_NO_MORE_SYMBOLS as the <<previous>> entry to get
  155.     the first one; returns BFD_NO_MORE_SYMBOLS when you're already
  156.     got the last one.
  157.  
  158.     A <<carsym>> is a canonical archive symbol.  The only
  159.     user-visible element is its name, a null-terminated string.
  160. */
  161.  
  162. symindex
  163. DEFUN(bfd_get_next_mapent,(abfd, prev, entry),
  164.      bfd *abfd AND
  165.      symindex prev AND
  166.      carsym **entry)
  167. {
  168.   if (!bfd_has_map (abfd)) {
  169.     bfd_error = invalid_operation;
  170.     return BFD_NO_MORE_SYMBOLS;
  171.   }
  172.   
  173.   if (prev == BFD_NO_MORE_SYMBOLS) prev = 0;
  174.   else if (++prev >= bfd_ardata (abfd)->symdef_count)
  175.     return BFD_NO_MORE_SYMBOLS;
  176.  
  177.   *entry = (bfd_ardata (abfd)->symdefs + prev);
  178.   return prev;
  179. }
  180.  
  181. /* To be called by backends only */
  182. bfd *
  183. _bfd_create_empty_archive_element_shell (obfd)
  184.      bfd *obfd;
  185. {
  186.   bfd *nbfd;
  187.  
  188.   nbfd = new_bfd_contained_in(obfd);
  189.   if (nbfd == NULL) {
  190.     bfd_error = no_memory;
  191.     return NULL;
  192.   }
  193.   return nbfd;
  194. }
  195.  
  196. /*
  197. FUNCTION
  198.     bfd_set_archive_head
  199.  
  200. SYNOPSIS
  201.     boolean bfd_set_archive_head(bfd *output, bfd *new_head);
  202.  
  203. DESCRIPTION
  204.     Used whilst processing archives. Sets the head of the chain of
  205.     BFDs contained in an archive to @var{new_head}. 
  206. */
  207.  
  208. boolean
  209. DEFUN(bfd_set_archive_head,(output_archive, new_head),
  210.      bfd *output_archive AND 
  211.      bfd *new_head)
  212. {
  213.  
  214.   output_archive->archive_head = new_head;
  215.   return true;
  216. }
  217.  
  218. bfd *
  219. look_for_bfd_in_cache (arch_bfd, filepos)
  220.      bfd *arch_bfd;
  221.      file_ptr filepos;
  222. {
  223.   struct ar_cache *current;
  224.  
  225.   for (current = bfd_ardata (arch_bfd)->cache; current != NULL;
  226.        current = current->next)
  227.     if (current->ptr == filepos) return current->arelt;
  228.  
  229.   return NULL;
  230. }
  231.  
  232. /* Kind of stupid to call cons for each one, but we don't do too many */
  233. boolean
  234. add_bfd_to_cache (arch_bfd, filepos, new_elt)
  235.      bfd *arch_bfd, *new_elt;
  236.      file_ptr filepos;
  237. {
  238.   struct ar_cache *new_cache = (struct ar_cache *)
  239.                 bfd_zalloc(arch_bfd, sizeof (struct ar_cache));
  240.  
  241.   if (new_cache == NULL) {
  242.     bfd_error = no_memory;
  243.     return false;
  244.   }
  245.  
  246.   new_cache->ptr = filepos;
  247.   new_cache->arelt = new_elt;
  248.   new_cache->next = (struct ar_cache *)NULL;
  249.   if (bfd_ardata (arch_bfd)->cache == NULL)
  250.     bfd_ardata (arch_bfd)->cache = new_cache;
  251.   else {
  252.     struct ar_cache *current = bfd_ardata (arch_bfd)->cache;
  253.  
  254.     for (; current->next != NULL; current = current->next);
  255.     current->next = new_cache;
  256.   }
  257.     
  258.   return true;
  259. }
  260.  
  261.  
  262.  
  263. /* The name begins with space.  Hence the rest of the name is an index into
  264.    the string table. */
  265. char *
  266. get_extended_arelt_filename (arch, name)
  267.      bfd *arch;
  268.      char *name;
  269. {
  270. #ifndef errno
  271.   extern int errno;
  272. #endif
  273.   unsigned long index = 0;
  274.  
  275.   /* Should extract string so that I can guarantee not to overflow into
  276.      the next region, but I"m too lazy. */
  277.   errno = 0;
  278.   index = strtol (name, NULL, 10);
  279.   if (errno != 0) {
  280.       bfd_error = malformed_archive;
  281.       return NULL;
  282.     }
  283.  
  284.   return bfd_ardata (arch)->extended_names + index;
  285. }  
  286.  
  287. /* This functions reads an arch header and returns an areltdata pointer, or
  288.    NULL on error.
  289.  
  290.    Presumes the file pointer is already in the right place (ie pointing
  291.    to the ar_hdr in the file).   Moves the file pointer; on success it
  292.    should be pointing to the front of the file contents; on failure it
  293.    could have been moved arbitrarily.
  294. */
  295.  
  296. struct areltdata *
  297. snarf_ar_hdr (abfd)
  298.      bfd *abfd;
  299. {
  300. #ifndef errno
  301.   extern int errno;
  302. #endif
  303.  
  304.     struct ar_hdr hdr;
  305.     char *hdrp = (char *) &hdr;
  306.     unsigned int parsed_size;
  307.     struct areltdata *ared;
  308.     char *filename = NULL;
  309.     unsigned int namelen = 0;
  310.     unsigned int allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
  311.     char *allocptr;
  312.  
  313.     if (bfd_read ((PTR)hdrp, 1, sizeof (struct ar_hdr), abfd)
  314.     != sizeof (struct ar_hdr)) {
  315.     bfd_error = no_more_archived_files;
  316.     return NULL;
  317.     }
  318.     if (strncmp ((hdr.ar_fmag), ARFMAG, 2)) {
  319.     bfd_error = malformed_archive;
  320.     return NULL;
  321.     }
  322.  
  323.     errno = 0;
  324.     parsed_size = strtol (hdr.ar_size, NULL, 10);
  325.     if (errno != 0) {
  326.     bfd_error = malformed_archive;
  327.     return NULL;
  328.     }
  329.  
  330.     /* extract the filename from the archive - there are two ways to
  331.        specify an extendend name table, either the first char of the
  332.        name is a space, or it's a slash  */
  333.     if ((hdr.ar_name[0] == '/' || hdr.ar_name[0] == ' ') 
  334.     && bfd_ardata (abfd)->extended_names != NULL) {
  335.     filename = get_extended_arelt_filename (abfd, hdr.ar_name);
  336.     if (filename == NULL) {
  337.         bfd_error = malformed_archive;
  338.         return NULL;
  339.     }
  340.     } 
  341.     else 
  342.     {
  343.         /* We judge the end of the name by looking for a space or a
  344.            padchar */
  345.  
  346.         namelen = 0;
  347.  
  348.         while (namelen < (unsigned)ar_maxnamelen(abfd) &&
  349.            ( hdr.ar_name[namelen] != 0 &&
  350.             hdr.ar_name[namelen] != ' ' &&
  351.             hdr.ar_name[namelen] != ar_padchar(abfd))) {
  352.         namelen++;
  353.         }
  354.  
  355.         allocsize += namelen + 1;
  356.     }
  357.  
  358.     allocptr = bfd_zalloc(abfd, allocsize);
  359.     if (allocptr == NULL) {
  360.     bfd_error = no_memory;
  361.     return NULL;
  362.     }
  363.  
  364.     ared = (struct areltdata *) allocptr;
  365.  
  366.     ared->arch_header = allocptr + sizeof (struct areltdata);
  367.     memcpy ((char *) ared->arch_header, (char *) &hdr, sizeof (struct ar_hdr));
  368.     ared->parsed_size = parsed_size;
  369.  
  370.     if (filename != NULL) ared->filename = filename;
  371.     else {
  372.     ared->filename = allocptr + (sizeof (struct areltdata) +
  373.                      sizeof (struct ar_hdr));
  374.     if (namelen)
  375.         memcpy (ared->filename, hdr.ar_name, namelen);
  376.     ared->filename[namelen] = '\0';
  377.     }
  378.   
  379.     return ared;
  380. }
  381.  
  382. /* This is an internal function; it's mainly used when indexing
  383.    through the archive symbol table, but also used to get the next
  384.    element, since it handles the bookkeeping so nicely for us.
  385. */
  386.  
  387. bfd *
  388. get_elt_at_filepos (archive, filepos)
  389.      bfd *archive;
  390.      file_ptr filepos;
  391. {
  392.   struct areltdata *new_areldata;
  393.   bfd *n_nfd;
  394.  
  395.   n_nfd = look_for_bfd_in_cache (archive, filepos);
  396.   if (n_nfd) return n_nfd;
  397.  
  398.   if (0 > bfd_seek (archive, filepos, SEEK_SET)) {
  399.     bfd_error = system_call_error;
  400.     return NULL;
  401.   }
  402.  
  403.   if ((new_areldata = snarf_ar_hdr (archive)) == NULL) return NULL;
  404.   
  405.   n_nfd = _bfd_create_empty_archive_element_shell (archive);
  406.   if (n_nfd == NULL) {
  407.     bfd_release (archive, (PTR)new_areldata);
  408.     return NULL;
  409.   }
  410.   n_nfd->origin = bfd_tell (archive);
  411.   n_nfd->arelt_data = (PTR) new_areldata;
  412.   n_nfd->filename = new_areldata->filename;
  413.  
  414.   if (add_bfd_to_cache (archive, filepos, n_nfd))
  415.     return n_nfd;
  416.  
  417.   /* huh? */
  418.   bfd_release (archive, (PTR)n_nfd);
  419.   bfd_release (archive, (PTR)new_areldata);
  420.   return NULL;
  421. }
  422.  
  423. /*
  424. FUNCTION
  425.     bfd_get_elt_at_index
  426.  
  427. SYNOPSIS
  428.     bfd *bfd_get_elt_at_index(bfd * archive, int index);
  429.  
  430. DESCRIPTION
  431.     Return the bfd which is referenced by the symbol indexed by <<index>>.
  432.     <<index>> should have been returned by <<bfd_get_next_mapent>> (q.v.).
  433.  
  434. */
  435. bfd *
  436. DEFUN(bfd_get_elt_at_index,(abfd, index),
  437.      bfd *abfd AND
  438.      int index)
  439. {
  440.   bfd *result =
  441.     get_elt_at_filepos
  442.       (abfd, (bfd_ardata (abfd)->symdefs + index)->file_offset);
  443.   return result;
  444. }
  445.  
  446. /*
  447. FUNCTION
  448.     bfd_openr_next_archived_file
  449.  
  450. SYNOPSIS
  451.     bfd* bfd_openr_next_archived_file(bfd *archive, bfd *previous);
  452.  
  453. DESCRIPTION
  454.     Initially provided a BFD containing an archive and NULL, opens
  455.     an inpout BFD on the first contained element and returns that.
  456.     Subsequent calls to bfd_openr_next_archived_file should pass
  457.     the archive and the previous return value to return a created
  458.     BFD to the next contained element. NULL is returned when there
  459.     are no more.
  460.  
  461. */
  462.  
  463. bfd *
  464. DEFUN(bfd_openr_next_archived_file,(archive, last_file),
  465.      bfd *archive AND  
  466.       bfd*last_file)
  467. {
  468.  
  469.     if ((bfd_get_format (archive) != bfd_archive) ||
  470.     (archive->direction == write_direction)) {
  471.         bfd_error = invalid_operation;
  472.         return NULL;
  473.     }
  474.  
  475.  
  476.     return BFD_SEND (archive,
  477.              openr_next_archived_file,
  478.              (archive,
  479.               last_file));
  480.  
  481. }
  482.  
  483. bfd *bfd_generic_openr_next_archived_file(archive, last_file)
  484.      bfd *archive;
  485.      bfd *last_file;
  486. {
  487.   file_ptr filestart;
  488.  
  489.   if (!last_file)
  490.     filestart = bfd_ardata (archive)->first_file_filepos;
  491.   else {
  492.     unsigned int size = arelt_size(last_file);
  493.     /* Pad to an even boundary... */
  494.     filestart = last_file->origin + size + size%2;
  495.   }
  496.  
  497.   return get_elt_at_filepos (archive, filestart);
  498. }
  499.  
  500.  
  501. bfd_target *
  502. bfd_generic_archive_p (abfd)
  503.      bfd *abfd;
  504. {
  505.   char armag[SARMAG+1];
  506.  
  507.   if (bfd_read ((PTR)armag, 1, SARMAG, abfd) != SARMAG) {
  508.     bfd_error = wrong_format;
  509.     return 0;
  510.   }
  511.  
  512. #ifdef GNU960
  513.   if (strncmp (armag, BFD_GNU960_ARMAG(abfd), SARMAG)) return 0;
  514. #else
  515.   if (strncmp (armag, ARMAG, SARMAG) &&
  516.       strncmp (armag, ARMAGB, SARMAG)) return 0;
  517. #endif
  518.  
  519.  
  520.  
  521.   /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
  522.      involves a cast, we can't do it as the left operand of assignment. */
  523.   abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc(abfd,sizeof (struct artdata));
  524.  
  525.   if (bfd_ardata (abfd)  == NULL) {
  526.     bfd_error = no_memory;
  527.     return 0;
  528.   }
  529.  
  530.   bfd_ardata (abfd)->first_file_filepos = SARMAG;
  531.   
  532.   if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd))) {
  533.     bfd_release(abfd, bfd_ardata (abfd));
  534.     abfd->tdata.aout_ar_data = NULL;
  535.     return 0;
  536.   }
  537.  
  538.   if (!BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd))) {
  539.     bfd_release(abfd, bfd_ardata (abfd));
  540.     abfd->tdata.aout_ar_data = NULL;
  541.     return 0;
  542.   }
  543.   
  544.   return abfd->xvec;
  545. }
  546.  
  547. /* Returns false on error, true otherwise */
  548. boolean
  549. bfd_slurp_bsd_armap (abfd)
  550.      bfd *abfd;
  551. {
  552.  
  553.     struct areltdata *mapdata;
  554.     char nextname[17];
  555.     unsigned int counter = 0;
  556.     int *raw_armap, *rbase;
  557.     struct artdata *ardata = bfd_ardata (abfd);
  558.     char *stringbase;
  559.  
  560.     /* FIXME, if the read fails, this routine quietly returns "true"!!
  561.        It should probably do that if the read gives 0 bytes (empty archive),
  562.        but fail for any other size... */
  563.     if (bfd_read ((PTR)nextname, 1, 16, abfd) == 16) {
  564.         /* The archive has at least 16 bytes in it */
  565.         bfd_seek (abfd, -16L, SEEK_CUR);
  566.  
  567.         /* This should be using RANLIBMAG, but at least it can be grepped for
  568.            in this comment.  */
  569.         if (strncmp (nextname, "__.SYMDEF       ", 16)) {
  570.             bfd_has_map (abfd) = false;
  571.             return true;
  572.         }
  573.  
  574.         mapdata = snarf_ar_hdr (abfd);
  575.         if (mapdata == NULL) return false;
  576.  
  577.         raw_armap = (int *) bfd_zalloc(abfd,mapdata->parsed_size);
  578.         if (raw_armap == NULL) {
  579.             bfd_error = no_memory;
  580.           byebye:
  581.             bfd_release (abfd, (PTR)mapdata);
  582.             return false;
  583.         }
  584.  
  585.         if (bfd_read ((PTR)raw_armap, 1, mapdata->parsed_size, abfd) !=
  586.         mapdata->parsed_size) {
  587.             bfd_error = malformed_archive;
  588.             bfd_release (abfd, (PTR)raw_armap);
  589.             goto byebye;
  590.         }
  591.  
  592.         ardata->symdef_count = bfd_h_get_32(abfd, (PTR)raw_armap) / sizeof (struct symdef);
  593.         ardata->cache = 0;
  594.         rbase = raw_armap+1;
  595.         ardata->symdefs = (carsym *) rbase;
  596.         stringbase = ((char *) (ardata->symdefs + ardata->symdef_count)) + 4;
  597.  
  598.         for (;counter < ardata->symdef_count; counter++) {
  599.             struct symdef *sym = ((struct symdef *) rbase) + counter;
  600.             sym->s.name = bfd_h_get_32(abfd, (PTR)(&(sym->s.string_offset))) + stringbase;
  601.             sym->file_offset = bfd_h_get_32(abfd, (PTR)( &(sym->file_offset)));
  602.         }
  603.   
  604.         ardata->first_file_filepos = bfd_tell (abfd);
  605.         /* Pad to an even boundary if you have to */
  606.         ardata->first_file_filepos += (ardata-> first_file_filepos) %2;
  607.         /* FIXME, we should provide some way to free raw_ardata when
  608.            we are done using the strings from it.  For now, it seems
  609.            to be allocated on an obstack anyway... */
  610.         bfd_has_map (abfd) = true;
  611.     }
  612.     return true;
  613. }
  614.  
  615. /* Returns false on error, true otherwise */
  616. boolean
  617. bfd_slurp_coff_armap (abfd)
  618.      bfd *abfd;
  619. {
  620.   struct areltdata *mapdata;
  621.   char nextname;
  622.   int *raw_armap, *rawptr;
  623.   struct artdata *ardata = bfd_ardata (abfd);
  624.   char *stringbase;
  625.   unsigned int stringsize;
  626.   carsym *carsyms;
  627.   int result;
  628.  
  629.   result = bfd_read ((PTR)&nextname, 1, 1, abfd);
  630.   bfd_seek (abfd, -1L, SEEK_CUR);
  631.  
  632.   if (result != 1 || nextname != '/') {
  633.     /* Actually I think this is an error for a COFF archive */
  634.     bfd_has_map (abfd) = false;
  635.     return true;
  636.   }
  637.  
  638.   mapdata = snarf_ar_hdr (abfd);
  639.   if (mapdata == NULL) return false;
  640.  
  641.   raw_armap = (int *) bfd_alloc(abfd,mapdata->parsed_size);
  642.  
  643.   if (raw_armap == NULL) 
  644.       {
  645.     bfd_error = no_memory;
  646.   byebye:
  647.     bfd_release (abfd, (PTR)mapdata);
  648.     return false;
  649.   }
  650.  
  651.   /* read in the raw map */
  652.   if (bfd_read ((PTR)raw_armap, 1, mapdata->parsed_size, abfd) !=
  653.       mapdata->parsed_size) {
  654.     bfd_error = malformed_archive;
  655.   oops:
  656.     bfd_release (abfd, (PTR)raw_armap);
  657.     goto byebye;
  658.   }
  659.  
  660.   /* The coff armap must be read sequentially.  So we construct a bsd-style
  661.      one in core all at once, for simplicity. 
  662.  
  663.      It seems that all numeric information in a coff archive is always
  664.      in big endian format, nomatter the host or target. */
  665.  
  666.   stringsize = mapdata->parsed_size - (4 * (_do_getb32((PTR)raw_armap))) - 4;
  667.  
  668.   {
  669.     unsigned int nsymz = _do_getb32( (PTR)raw_armap);
  670.     unsigned int carsym_size = (nsymz * sizeof (carsym));
  671.     unsigned int ptrsize = (4 * nsymz);
  672.     unsigned int i;
  673.     ardata->symdefs = (carsym *) bfd_zalloc(abfd,carsym_size + stringsize + 1);
  674.     if (ardata->symdefs == NULL) {
  675.       bfd_error = no_memory;
  676.       goto oops;
  677.     }
  678.     carsyms = ardata->symdefs;
  679.  
  680.     stringbase = ((char *) ardata->symdefs) + carsym_size;
  681.     memcpy (stringbase, (char*)raw_armap + ptrsize + 4,  stringsize);
  682.  
  683.  
  684.     /* OK, build the carsyms */
  685.     for (i = 0; i < nsymz; i++) 
  686.       {
  687.     rawptr = raw_armap + i + 1;
  688.     carsyms->file_offset = _do_getb32((PTR)rawptr);
  689.     carsyms->name = stringbase;
  690.     for (; *(stringbase++););
  691.     carsyms++;
  692.       }
  693.     *stringbase = 0;
  694.   }
  695.   ardata->symdef_count = _do_getb32((PTR)raw_armap);
  696.   ardata->first_file_filepos = bfd_tell (abfd);
  697.   /* Pad to an even boundary if you have to */
  698.   ardata->first_file_filepos += (ardata->first_file_filepos) %2;
  699.  
  700.   /* We'd like to release these allocations, but we have allocated stuff
  701.      since then (using the same obstack, if bfd_release is obstack based).
  702.      So they will stick around until the BFD is closed.  */
  703.   /*  bfd_release (abfd, (PTR)raw_armap);
  704.       bfd_release (abfd, (PTR)mapdata);  */
  705.   bfd_has_map (abfd) = true;
  706.   return true;
  707. }
  708.  
  709. /** Extended name table.
  710.  
  711.   Normally archives support only 14-character filenames.
  712.  
  713.   Intel has extended the format: longer names are stored in a special
  714.   element (the first in the archive, or second if there is an armap);
  715.   the name in the ar_hdr is replaced by <space><index into filename
  716.   element>.  Index is the P.R. of an int (decimal).  Data General have
  717.   extended the format by using the prefix // for the special element */
  718.  
  719. /* Returns false on error, true otherwise */
  720. boolean
  721. _bfd_slurp_extended_name_table (abfd)
  722.      bfd *abfd;
  723. {
  724.   char nextname[17];
  725.   struct areltdata *namedata;
  726.  
  727.   /* FIXME:  Formatting sucks here, and in case of failure of BFD_READ,
  728.      we probably don't want to return true.  */
  729.   if (bfd_read ((PTR)nextname, 1, 16, abfd) == 16) {
  730.  
  731.     bfd_seek (abfd, -16L, SEEK_CUR);
  732.  
  733.     if (strncmp (nextname, "ARFILENAMES/    ", 16) != 0 &&
  734.     strncmp (nextname, "//              ", 16) != 0) 
  735.     {
  736.       bfd_ardata (abfd)->extended_names = NULL;
  737.       return true;
  738.     }
  739.  
  740.     namedata = snarf_ar_hdr (abfd);
  741.     if (namedata == NULL) return false;
  742.   
  743.     bfd_ardata (abfd)->extended_names = bfd_zalloc(abfd,namedata->parsed_size);
  744.     if (bfd_ardata (abfd)->extended_names == NULL) {
  745.       bfd_error = no_memory;
  746.     byebye:
  747.       bfd_release (abfd, (PTR)namedata);
  748.       return false;
  749.     }
  750.  
  751.     if (bfd_read ((PTR)bfd_ardata (abfd)->extended_names, 1,
  752.           namedata->parsed_size, abfd) != namedata->parsed_size) {
  753.       bfd_error = malformed_archive;
  754.       bfd_release (abfd, (PTR)(bfd_ardata (abfd)->extended_names));
  755.       bfd_ardata (abfd)->extended_names = NULL;
  756.       goto byebye;
  757.     }
  758.  
  759.     /* Since the archive is supposed to be printable if it contains
  760.        text, the entries in the list are newline-padded, not null
  761.        padded. We'll fix that there..  */
  762.       {
  763.     char *temp = bfd_ardata (abfd)->extended_names;
  764.     for (; *temp != '\0'; ++temp)
  765.       if (*temp == '\n') *temp = '\0';
  766.       }
  767.   
  768.     /* Pad to an even boundary if you have to */
  769.     bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd);
  770.     bfd_ardata (abfd)->first_file_filepos +=
  771.       (bfd_ardata (abfd)->first_file_filepos) %2;
  772.  
  773.     /* FIXME, we can't release namedata here because it was allocated
  774.        below extended_names on the obstack... */
  775.     /* bfd_release (abfd, namedata); */
  776.   }
  777.   return true;
  778. }
  779.  
  780. #ifdef VMS
  781.  
  782. /* Return a copy of the stuff in the filename between any :]> and a
  783.    semicolon */
  784. static CONST char *
  785. DEFUN(normalize,(file),
  786.       CONST char *file)
  787. {
  788.   CONST char *first;
  789.   CONST char *last;
  790.   char *copy;
  791.  
  792.   first = file + strlen(file)-1;
  793.   last = first+1;
  794.  
  795.   while (first != file) 
  796.   {
  797.     if (*first == ';') 
  798.      last = first;
  799.     if (*first == ':' || *first == ']' ||*first == '>') 
  800.     { 
  801.       first++;
  802.       break;
  803.     }
  804.     first --;
  805.   }
  806.   
  807.  
  808.   copy = malloc(last - first + 1);
  809.   memcpy(copy, first, last-first);
  810.   copy[last-first] = 0;
  811.  
  812.   return copy;
  813. }
  814.  
  815. #else
  816. static
  817. char *normalize(file)
  818. char *file;
  819. {
  820.   char *    filename = strrchr(file, '/');
  821.  
  822.   if (filename != (char *)NULL) {
  823.       filename ++;
  824.     }
  825.   else {
  826.       filename = file;
  827.     }
  828.   return filename;
  829. }
  830. #endif
  831. /* Follows archive_head and produces an extended name table if necessary.
  832.    Returns (in tabloc) a pointer to an extended name table, and in tablen
  833.    the length of the table.  If it makes an entry it clobbers the filename
  834.    so that the element may be written without further massage.
  835.    Returns true if it ran successfully, false if something went wrong.
  836.    A successful return may still involve a zero-length tablen!
  837.    */
  838. boolean
  839. bfd_construct_extended_name_table (abfd, tabloc, tablen)
  840.      bfd *abfd;
  841.      char **tabloc;
  842.      unsigned int *tablen;
  843. {
  844.   unsigned int maxname = abfd->xvec->ar_max_namelen;
  845.   unsigned int total_namelen = 0;
  846.   bfd *current;
  847.   char *strptr;
  848.  
  849.   *tablen = 0;
  850.   
  851.   /* Figure out how long the table should be */
  852.   for (current = abfd->archive_head; current != NULL; current = current->next){
  853.     unsigned int thislen = strlen (normalize(current->filename));
  854.     if (thislen > maxname) total_namelen += thislen + 1; /* leave room for \n */
  855.   }
  856.  
  857.   if (total_namelen == 0) return true;
  858.  
  859.   *tabloc = bfd_zalloc (abfd,total_namelen);
  860.   if (*tabloc == NULL) {
  861.     bfd_error = no_memory;
  862.     return false;
  863.   }
  864.  
  865.   *tablen = total_namelen;
  866.   strptr = *tabloc;
  867.  
  868.   for (current = abfd->archive_head; current != NULL; current =
  869.        current->next) {
  870.     CONST char *normal =normalize( current->filename);
  871.     unsigned int thislen = strlen (normal);
  872.     if (thislen > maxname) {
  873.       /* Works for now; may need to be re-engineered if we encounter an oddball
  874.      archive format and want to generalise this hack. */
  875.       struct ar_hdr *hdr = arch_hdr(current);
  876.       strcpy (strptr, normal);
  877.       strptr[thislen] = '\n';
  878.       hdr->ar_name[0] = ' ';
  879.       /* We know there will always be enough room (one of the few cases
  880.      where you may safely use sprintf). */
  881.       sprintf ((hdr->ar_name) + 1, "%-d", (unsigned) (strptr - *tabloc));
  882.       /* Kinda Kludgy.   We should just use the returned value of sprintf
  883.      but not all implementations get this right */
  884.     {
  885.       char *temp = hdr->ar_name +2; 
  886.       for (; temp < hdr->ar_name + maxname; temp++)
  887.         if (*temp == '\0') *temp = ' ';
  888.     }
  889.       strptr += thislen + 1;
  890.     }
  891.   }
  892.  
  893.   return true;
  894. }
  895.  
  896. /** A couple of functions for creating ar_hdrs */
  897.  
  898. /* Takes a filename, returns an arelt_data for it, or NULL if it can't make one.
  899.    The filename must refer to a filename in the filesystem.
  900.    The filename field of the ar_hdr will NOT be initialized
  901. */
  902.  
  903. struct areltdata *
  904. DEFUN(bfd_ar_hdr_from_filesystem, (abfd,filename),
  905.       bfd* abfd AND
  906.       CONST char *filename)
  907. {
  908.   struct stat status;
  909.   struct areltdata *ared;
  910.   struct ar_hdr *hdr;
  911.   char *temp, *temp1;
  912.  
  913.  
  914.   if (stat (filename, &status) != 0) {
  915.     bfd_error = system_call_error;
  916.     return NULL;
  917.   }
  918.  
  919.   ared = (struct areltdata *) bfd_zalloc(abfd, sizeof (struct ar_hdr) +
  920.                       sizeof (struct areltdata));
  921.   if (ared == NULL) {
  922.     bfd_error = no_memory;
  923.     return NULL;
  924.   }
  925.   hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
  926.  
  927.   /* ar headers are space padded, not null padded! */
  928.   temp = (char *) hdr;
  929.   temp1 = temp + sizeof (struct ar_hdr) - 2;
  930.   for (; temp < temp1; *(temp++) = ' ');
  931.   strncpy (hdr->ar_fmag, ARFMAG, 2);
  932.   
  933.   /* Goddamned sprintf doesn't permit MAXIMUM field lengths */
  934.   sprintf ((hdr->ar_date), "%-12ld", status.st_mtime);
  935.   sprintf ((hdr->ar_uid), "%d", status.st_uid);
  936.   sprintf ((hdr->ar_gid), "%d", status.st_gid);
  937.   sprintf ((hdr->ar_mode), "%-8o", (unsigned) status.st_mode);
  938.   sprintf ((hdr->ar_size), "%-10ld", status.st_size);
  939.   /* Correct for a lossage in sprintf whereby it null-terminates.  I cannot
  940.      understand how these C losers could design such a ramshackle bunch of
  941.      IO operations */
  942.   temp = (char *) hdr;
  943.   temp1 = temp + sizeof (struct ar_hdr) - 2;
  944.   for (; temp < temp1; temp++) {
  945.     if (*temp == '\0') *temp = ' ';
  946.   }
  947.   strncpy (hdr->ar_fmag, ARFMAG, 2);
  948.   ared->parsed_size = status.st_size;
  949.   ared->arch_header = (char *) hdr;
  950.  
  951.   return ared;
  952. }
  953.  
  954. /* This is magic required by the "ar" program.  Since it's
  955.     undocumented, it's undocumented.   You may think that it would
  956.     take a strong stomach to write this, and it does, but it takes
  957.     even a stronger stomach to try to code around such a thing!
  958. */
  959.  
  960. struct ar_hdr *
  961. DEFUN(bfd_special_undocumented_glue, (abfd, filename),
  962.       bfd *abfd AND
  963.       char *filename)
  964. {
  965.   struct areltdata *ar_elt = bfd_ar_hdr_from_filesystem (abfd, filename);
  966.   if (ar_elt == NULL)
  967.       return NULL;
  968.   return (struct ar_hdr *) ar_elt->arch_header;
  969. }
  970.  
  971.  
  972. /* Analogous to stat call */
  973. int
  974. bfd_generic_stat_arch_elt (abfd, buf)
  975.      bfd *abfd;
  976.      struct stat *buf;
  977. {
  978.   struct ar_hdr *hdr;
  979.   char *aloser;
  980.   
  981.   if (abfd->arelt_data == NULL) {
  982.     bfd_error = invalid_operation;
  983.     return -1;
  984.   }
  985.     
  986.   hdr = arch_hdr (abfd);
  987.  
  988. #define foo(arelt, stelt, size)  \
  989.   buf->stelt = strtol (hdr->arelt, &aloser, size); \
  990.   if (aloser == hdr->arelt) return -1;
  991.   
  992.   foo (ar_date, st_mtime, 10);
  993.   foo (ar_uid, st_uid, 10);
  994.   foo (ar_gid, st_gid, 10);
  995.   foo (ar_mode, st_mode, 8);
  996.   foo (ar_size, st_size, 10);
  997.  
  998.   return 0;
  999. }
  1000.  
  1001. void
  1002. bfd_dont_truncate_arname (abfd, pathname, arhdr)
  1003.      bfd *abfd;
  1004.      CONST char *pathname;
  1005.      char *arhdr;
  1006. {
  1007.   /* FIXME: This interacts unpleasantly with ar's quick-append option.
  1008.      Fortunately ic960 users will never use that option.  Fixing this
  1009.      is very hard; fortunately I know how to do it and will do so once
  1010.      intel's release is out the door. */
  1011.    
  1012.   struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
  1013.   int length;
  1014.   CONST char *filename = normalize(pathname);
  1015.   int maxlen = ar_maxnamelen (abfd);
  1016.  
  1017.   length = strlen (filename);
  1018.  
  1019.   if (length <= maxlen)
  1020.     memcpy (hdr->ar_name, filename, length);
  1021.  
  1022.   if (length < maxlen) (hdr->ar_name)[length] = ar_padchar (abfd);
  1023.   return;
  1024.  
  1025. }
  1026.  
  1027. void
  1028. bfd_bsd_truncate_arname (abfd, pathname, arhdr)
  1029.      bfd *abfd;
  1030.      CONST char *pathname;
  1031.      char *arhdr;
  1032. {
  1033.   struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
  1034.   int length;
  1035.   CONST char *filename = strrchr (pathname, '/');
  1036.   int maxlen = ar_maxnamelen (abfd);
  1037.  
  1038.  
  1039.   if (filename == NULL)
  1040.     filename = pathname;
  1041.   else
  1042.     ++filename;
  1043.  
  1044.   length = strlen (filename);
  1045.  
  1046.   if (length <= maxlen)
  1047.     memcpy (hdr->ar_name, filename, length);
  1048.   else {
  1049.     /* pathname: meet procrustes */
  1050.     memcpy (hdr->ar_name, filename, maxlen);
  1051.     length = maxlen;
  1052.   }
  1053.  
  1054.   if (length < maxlen) (hdr->ar_name)[length] = ar_padchar (abfd);
  1055. }
  1056.  
  1057. /* Store name into ar header.  Truncates the name to fit.
  1058.    1> strip pathname to be just the basename.
  1059.    2> if it's short enuf to fit, stuff it in.
  1060.    3> If it doesn't end with .o, truncate it to fit
  1061.    4> truncate it before the .o, append .o, stuff THAT in.
  1062. */
  1063.  
  1064. /* This is what gnu ar does.  It's better but incompatible with the bsd ar. */
  1065. void
  1066. bfd_gnu_truncate_arname (abfd, pathname, arhdr)
  1067.      bfd *abfd;
  1068.      CONST char *pathname;
  1069.      char *arhdr;
  1070. {
  1071.   struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
  1072.   int length;
  1073.   CONST char *filename = strrchr (pathname, '/');
  1074.   int maxlen = ar_maxnamelen (abfd);
  1075.     
  1076.   if (filename == NULL)
  1077.     filename = pathname;
  1078.   else
  1079.     ++filename;
  1080.  
  1081.   length = strlen (filename);
  1082.  
  1083.   if (length <= maxlen)
  1084.     memcpy (hdr->ar_name, filename, length);
  1085.   else {            /* pathname: meet procrustes */
  1086.     memcpy (hdr->ar_name, filename, maxlen);
  1087.     if ((filename[length - 2] == '.') && (filename[length - 1] == 'o')) {
  1088.       hdr->ar_name[maxlen - 2] = '.';
  1089.       hdr->ar_name[maxlen - 1] = 'o';
  1090.     }
  1091.     length = maxlen;
  1092.   }
  1093.  
  1094.   if (length < 16) (hdr->ar_name)[length] = ar_padchar (abfd);
  1095. }
  1096.  
  1097.  
  1098. PROTO (boolean, compute_and_write_armap, (bfd *arch, unsigned int elength));
  1099.  
  1100. /* The BFD is open for write and has its format set to bfd_archive */
  1101. boolean
  1102. _bfd_write_archive_contents (arch)
  1103.      bfd *arch;
  1104. {
  1105.   bfd *current;
  1106.   char *etable = NULL;
  1107.   unsigned int elength = 0;
  1108.   boolean makemap = bfd_has_map (arch);
  1109.   boolean hasobjects = false;    /* if no .o's, don't bother to make a map */
  1110.   unsigned int i;
  1111.  
  1112.   /* Verify the viability of all entries; if any of them live in the
  1113.      filesystem (as opposed to living in an archive open for input)
  1114.      then construct a fresh ar_hdr for them.
  1115.      */
  1116.   for (current = arch->archive_head; current; current = current->next) {
  1117.     if (bfd_write_p (current)) {
  1118.       bfd_error = invalid_operation;
  1119.       return false;
  1120.     }
  1121.     if (!current->arelt_data) {
  1122.       current->arelt_data =
  1123.       (PTR) bfd_ar_hdr_from_filesystem (arch, current->filename);
  1124.       if (!current->arelt_data) return false;
  1125.  
  1126.       /* Put in the file name */
  1127.     
  1128.     BFD_SEND (arch, _bfd_truncate_arname,(arch, 
  1129.                       current->filename,
  1130.                      (char *) arch_hdr(current)));
  1131.  
  1132.       
  1133.     }
  1134.  
  1135.     if (makemap) {        /* don't bother if we won't make a map! */
  1136.       if ((bfd_check_format (current, bfd_object))
  1137. #if 0                /* FIXME -- these are not set correctly */
  1138.       && ((bfd_get_file_flags (current) & HAS_SYMS))
  1139. #endif
  1140.       )
  1141.     hasobjects = true;
  1142.     }
  1143.   }
  1144.  
  1145.   if (!bfd_construct_extended_name_table (arch, &etable, &elength))
  1146.     return false;
  1147.  
  1148.   bfd_seek (arch, 0, SEEK_SET);
  1149. #ifdef GNU960
  1150.   bfd_write (BFD_GNU960_ARMAG(arch), 1, SARMAG, arch);
  1151. #else
  1152.   bfd_write (ARMAG, 1, SARMAG, arch);
  1153. #endif
  1154.  
  1155.   if (makemap && hasobjects) {
  1156.  
  1157.     if (compute_and_write_armap (arch, elength) != true) {
  1158.       return false;
  1159.     }
  1160.   }
  1161.  
  1162.   if (elength != 0) {
  1163.     struct ar_hdr hdr;
  1164.  
  1165.     memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
  1166.     sprintf (&(hdr.ar_name[0]), "ARFILENAMES/");
  1167.     sprintf (&(hdr.ar_size[0]), "%-10d", (int) elength);
  1168.     hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
  1169.     for (i = 0; i < sizeof (struct ar_hdr); i++)
  1170.       if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
  1171.     bfd_write ((char *)&hdr, 1, sizeof (struct ar_hdr), arch);
  1172.     bfd_write (etable, 1, elength, arch);
  1173.     if ((elength % 2) == 1) bfd_write ("\n", 1, 1, arch);
  1174.  
  1175.   }
  1176.  
  1177.   for (current = arch->archive_head; current; current = current->next) {
  1178.     char buffer[DEFAULT_BUFFERSIZE];
  1179.     unsigned int remaining = arelt_size (current);
  1180.     struct ar_hdr *hdr = arch_hdr(current);
  1181.     /* write ar header */
  1182.  
  1183.     if (bfd_write ((char *)hdr, 1, sizeof(*hdr), arch) != sizeof(*hdr)) {
  1184.     syserr:
  1185.     bfd_error = system_call_error;
  1186.     return false;
  1187.       }
  1188.     if (bfd_seek (current, 0L, SEEK_SET) != 0L) goto syserr;
  1189.     while (remaining) 
  1190.     {
  1191.       unsigned int amt = DEFAULT_BUFFERSIZE;
  1192.       if (amt > remaining) {
  1193.         amt = remaining;
  1194.       }
  1195.       if (bfd_read (buffer, amt, 1, current) != amt) goto syserr;
  1196.       if (bfd_write (buffer, amt, 1, arch)   != amt) goto syserr;
  1197.       remaining -= amt;
  1198.     }
  1199.     if ((arelt_size (current) % 2) == 1) bfd_write ("\n", 1, 1, arch);
  1200.   }
  1201. return true;
  1202. }
  1203.  
  1204. /* Note that the namidx for the first symbol is 0 */
  1205.  
  1206. boolean
  1207. compute_and_write_armap (arch, elength)
  1208.      bfd *arch;
  1209.      unsigned int elength;
  1210. {
  1211.     bfd *current;
  1212.     file_ptr elt_no = 0;
  1213.     struct orl *map;
  1214.     int orl_max = 15000;    /* fine initial default */
  1215.     int orl_count = 0;
  1216.     int stridx = 0;        /* string index */
  1217.  
  1218.     /* Dunno if this is the best place for this info... */
  1219.     if (elength != 0) elength += sizeof (struct ar_hdr);
  1220.     elength += elength %2 ;
  1221.  
  1222.     map = (struct orl *) bfd_zalloc (arch,orl_max * sizeof (struct orl));
  1223.     if (map == NULL) {
  1224.         bfd_error = no_memory;
  1225.         return false;
  1226.     }
  1227.  
  1228.     /* Drop all the files called __.SYMDEF, we're going to make our
  1229.        own */
  1230.     while (arch->archive_head   &&
  1231.        strcmp(arch->archive_head->filename,"__.SYMDEF") == 0) 
  1232.     {
  1233.     arch->archive_head = arch->archive_head->next;
  1234.     }
  1235.     /* Map over each element */
  1236.     for (current = arch->archive_head;
  1237.      current != (bfd *)NULL;
  1238.      current = current->next, elt_no++) 
  1239.     {
  1240.     if ((bfd_check_format (current, bfd_object) == true)
  1241.         && ((bfd_get_file_flags (current) & HAS_SYMS))) {
  1242.         asymbol **syms;
  1243.         unsigned int storage;
  1244.         unsigned int symcount;
  1245.         unsigned int src_count;
  1246.  
  1247.         storage = get_symtab_upper_bound (current);
  1248.         if (storage != 0) {
  1249.  
  1250.             syms = (asymbol **) bfd_zalloc (arch,storage);
  1251.             if (syms == NULL) {
  1252.                 bfd_error = no_memory; /* FIXME -- memory leak */
  1253.                 return false;
  1254.                 }
  1255.             symcount = bfd_canonicalize_symtab (current, syms);
  1256.  
  1257.  
  1258.             /* Now map over all the symbols, picking out the ones we want */
  1259.             for (src_count = 0; src_count <symcount; src_count++) {
  1260.                 flagword flags =
  1261.                  (syms[src_count])->flags;
  1262.                 asection  *sec =
  1263.                  syms[src_count]->section;
  1264.                 
  1265.                 if ((flags & BSF_GLOBAL) ||
  1266.                     (flags & BSF_INDIRECT) ||
  1267.                     (sec == &bfd_com_section)) {
  1268.  
  1269.                     /* This symbol will go into the archive header */
  1270.                     if (orl_count == orl_max) 
  1271.                     {
  1272.                         orl_max *= 2;
  1273.                         map = (struct orl *) bfd_realloc (arch, (char *) map,
  1274.                                           orl_max * sizeof (struct orl));
  1275.                     }
  1276.  
  1277.                     (map[orl_count]).name = (char **) &((syms[src_count])->name);
  1278.                     (map[orl_count]).pos = (file_ptr) current;
  1279.                     (map[orl_count]).namidx = stridx;
  1280.  
  1281.                     stridx += strlen ((syms[src_count])->name) + 1;
  1282.                     ++orl_count;
  1283.                     }
  1284.                 }
  1285.             }
  1286.         }
  1287.     }
  1288.     /* OK, now we have collected all the data, let's write them out */
  1289.     if (!BFD_SEND (arch, write_armap,
  1290.            (arch, elength, map, orl_count, stridx))) {
  1291.  
  1292.         return false;
  1293.     }
  1294.  
  1295.  
  1296.     return true;
  1297. }
  1298.  
  1299. boolean
  1300. bsd_write_armap (arch, elength, map, orl_count, stridx)
  1301.      bfd *arch;
  1302.      unsigned int elength;
  1303.      struct orl *map;
  1304.      unsigned int orl_count;
  1305.      int stridx;
  1306. {
  1307.   int padit = stridx & 1;
  1308.   unsigned int ranlibsize = orl_count * sizeof (struct ranlib);
  1309.   unsigned int stringsize = stridx + padit;
  1310.   /* Include 8 bytes to store ranlibsize and stringsize in output. */
  1311.   unsigned int mapsize = ranlibsize + stringsize + 8;
  1312.   file_ptr firstreal;
  1313.   bfd *current = arch->archive_head;
  1314.   bfd *last_elt = current;        /* last element arch seen */
  1315.   int temp;
  1316.   int count;
  1317.   struct ar_hdr hdr;
  1318.   struct stat statbuf;
  1319.   unsigned int i;
  1320.  
  1321.   firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
  1322.  
  1323.   stat (arch->filename, &statbuf);
  1324.   memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
  1325.   sprintf (hdr.ar_name, RANLIBMAG);
  1326.   sprintf (hdr.ar_date, "%ld", statbuf.st_mtime);  
  1327.   sprintf (hdr.ar_uid, "%d", getuid());
  1328.   sprintf (hdr.ar_gid, "%d", getgid());
  1329.   sprintf (hdr.ar_size, "%-10d", (int) mapsize);
  1330.   hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
  1331.   for (i = 0; i < sizeof (struct ar_hdr); i++)
  1332.     if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
  1333.   bfd_write ((char *)&hdr, 1, sizeof (struct ar_hdr), arch);
  1334.   bfd_h_put_32(arch, ranlibsize, (PTR)&temp);
  1335.   bfd_write (&temp, 1, sizeof (temp), arch);
  1336.   
  1337.   for (count = 0; count < orl_count; count++) {
  1338.     struct symdef outs;
  1339.     struct symdef *outp = &outs;
  1340.     
  1341.     if (((bfd *)(map[count]).pos) != last_elt) {
  1342.         do {
  1343.             firstreal += arelt_size (current) + sizeof (struct ar_hdr);
  1344.             firstreal += firstreal % 2;
  1345.             current = current->next;
  1346.         } while (current != (bfd *)(map[count]).pos);
  1347.     } /* if new archive element */
  1348.  
  1349.     last_elt = current;
  1350.     bfd_h_put_32(arch, ((map[count]).namidx),(PTR) &outs.s.string_offset);
  1351.     bfd_h_put_32(arch, firstreal,(PTR) &outs.file_offset);
  1352.     bfd_write ((char *)outp, 1, sizeof (outs), arch);
  1353.   }
  1354.  
  1355.   /* now write the strings themselves */
  1356.   bfd_h_put_32(arch, stringsize, (PTR)&temp);
  1357.   bfd_write ((PTR)&temp, 1, sizeof (temp), arch);
  1358.   for (count = 0; count < orl_count; count++)
  1359.     bfd_write (*((map[count]).name), 1, strlen (*((map[count]).name))+1, arch);
  1360.  
  1361.   /* The spec sez this should be a newline.  But in order to be
  1362.      bug-compatible for sun's ar we use a null. */
  1363.   if (padit)
  1364.     bfd_write("\0",1,1,arch);
  1365.  
  1366.   return true;
  1367. }
  1368.  
  1369.  
  1370. /* A coff armap looks like :
  1371.  lARMAG
  1372.  struct ar_hdr with name = '/' 
  1373.  number of symbols
  1374.  offset of file for symbol 0
  1375.  offset of file for symbol 1
  1376.  
  1377.  offset of file for symbol n-1
  1378.  symbol name 0
  1379.  symbol name 1    
  1380.  
  1381.  symbol name n-1
  1382.  
  1383. */
  1384.  
  1385. boolean
  1386. coff_write_armap (arch, elength, map, symbol_count, stridx)
  1387.      bfd *arch;
  1388.      unsigned int elength;
  1389.      struct orl *map;
  1390.      unsigned int symbol_count;
  1391.      int stridx;
  1392. {
  1393.     /* The size of the ranlib is the number of exported symbols in the
  1394.        archive * the number of bytes in a int, + an int for the count */
  1395.  
  1396.     unsigned int ranlibsize = (symbol_count * 4) + 4;
  1397.     unsigned int stringsize = stridx;
  1398.     unsigned int mapsize = stringsize + ranlibsize;
  1399.     file_ptr archive_member_file_ptr;
  1400.     bfd *current = arch->archive_head;
  1401.     int count;
  1402.     struct ar_hdr hdr;
  1403.     unsigned int i;
  1404.     int padit = mapsize & 1;
  1405.   
  1406.     if (padit) mapsize ++;
  1407.  
  1408.     /* work out where the first object file will go in the archive */
  1409.     archive_member_file_ptr =   mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
  1410.  
  1411.     memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
  1412.     hdr.ar_name[0] = '/';
  1413.     sprintf (hdr.ar_size, "%-10d", (int) mapsize);
  1414.     sprintf (hdr.ar_date, "%ld", (long)time (NULL));
  1415.     /* This, at least, is what Intel coff sets the values to.: */
  1416.     sprintf ((hdr.ar_uid), "%d", 0);
  1417.     sprintf ((hdr.ar_gid), "%d", 0);
  1418.     sprintf ((hdr.ar_mode), "%-7o",(unsigned ) 0);
  1419.     hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
  1420.  
  1421.     for (i = 0; i < sizeof (struct ar_hdr); i++)
  1422.      if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
  1423.  
  1424.     /* Write the ar header for this item and the number of symbols */
  1425.  
  1426.   
  1427.     bfd_write ((PTR)&hdr, 1, sizeof (struct ar_hdr), arch);
  1428.  
  1429.     bfd_write_bigendian_4byte_int(arch, symbol_count);
  1430.  
  1431.     /* Two passes, first write the file offsets for each symbol -
  1432.        remembering that each offset is on a two byte boundary.  */
  1433.  
  1434.     /* Write out the file offset for the file associated with each
  1435.        symbol, and remember to keep the offsets padded out.  */
  1436.  
  1437.     current = arch->archive_head;
  1438.     count = 0;
  1439.     while (current != (bfd *)NULL && count < symbol_count) {
  1440.     /* For each symbol which is used defined in this object, write out
  1441.        the object file's address in the archive */
  1442.     
  1443.     while (((bfd *)(map[count]).pos) == current) {
  1444.         bfd_write_bigendian_4byte_int(arch, archive_member_file_ptr);
  1445.         count++;
  1446.     }
  1447.     /* Add size of this archive entry */
  1448.     archive_member_file_ptr += arelt_size (current) + sizeof (struct
  1449.                                   ar_hdr);
  1450.     /* remember aboout the even alignment */
  1451.     archive_member_file_ptr += archive_member_file_ptr % 2;
  1452.     current = current->next;
  1453.     }  
  1454.  
  1455.  
  1456.  
  1457.     /* now write the strings themselves */
  1458.     for (count = 0; count < symbol_count; count++) {
  1459.     bfd_write ((PTR)*((map[count]).name),
  1460.            1,
  1461.            strlen (*((map[count]).name))+1, arch);
  1462.  
  1463.     }
  1464.     /* The spec sez this should be a newline.  But in order to be
  1465.        bug-compatible for arc960 we use a null. */
  1466.     if (padit)
  1467.      bfd_write("\0",1,1,arch);
  1468.  
  1469.     return true;
  1470. }
  1471.